﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework.Media;

namespace SpeedRun
{
    public partial class SongSelector : PhoneApplicationPage
    {
        List<Song> songs;
        static Song thisSong;
        SongCollection songColl;
        public SongSelector()
        {
            InitializeComponent();
        }

        private void ListBox_Loaded(object sender, RoutedEventArgs e)
        {
            MediaLibrary m = new MediaLibrary();            
                songColl = m.Songs;
                songs = m.Songs.ToList();
                foreach (Song song in songs)
                {
                    songCollection.Items.Add(new SongItem()
                    {
                        Name = song.Name,
                        Artist = song.Artist.ToString()
                    });
                }
            
        }

        private void Image_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {

        }

        private void TextBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            thisSong = songs.FirstOrDefault();
            foreach (Song song in songs)
            {
                if (song.Name == ((sender as TextBlock).Text.ToString()))
                {
                    thisSong = song;
                    break;
                }                
            }

            
            MediaPlayer.Play(songColl, songs.IndexOf(thisSong));
        }

        private void StackPanel_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            var obj = ((sender as StackPanel).Children[1] as StackPanel).Children[0];
            TextBlock_Tap(obj, e);
            
            songCollection.SelectedItem = sender;
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            string query = (sender as TextBox).Text.Trim();
            try
            {
                if (query != null)
                {
                    List<Song> results = new List<Song>();
                    foreach (Song song in songs)
                    {
                        if (song.Name.ToLowerInvariant().Trim().Contains(query))
                        {
                            results.Add(song);
                        }
                    }

                    if (results.Count == 0)
                        foreach (Song song in songs)
                        {
                            if (song.Artist.Name.ToLowerInvariant().Trim().Contains(query))
                            {
                                results.Add(song);
                            }
                        }

                    if (results.Count == 0)
                        foreach (Song song in songs)
                        {
                            if (song.Album.Name.ToLowerInvariant().Trim().Contains(query))
                            {
                                results.Add(song);
                            }
                        }


                    SearchResults.ItemsSource = results;
                    SearchResults.Visibility = System.Windows.Visibility.Visible;
                    songCollection.Visibility = System.Windows.Visibility.Collapsed;
                }

                else
                {
                    SearchResults.Visibility = System.Windows.Visibility.Collapsed;
                    songCollection.Visibility = System.Windows.Visibility.Visible;
                }
            }
            catch (Exception _e)
            {
                SearchResults.Visibility = System.Windows.Visibility.Collapsed;
                songCollection.Visibility = System.Windows.Visibility.Visible;
            }
        }

        private void TextBox_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {
            string query = (sender as TextBox).Text.Trim();
            try
            {
                if (query != null)
                {
                    List<Song> results = new List<Song>();
                    foreach (Song song in songs)
                    {
                        if (song.Name.ToLowerInvariant().Trim().Contains(query))
                        {
                            results.Add(song);
                        }
                    }

                    if(results.Count == 0)
                        foreach (Song song in songs)
                        {
                            if (song.Artist.Name.ToLowerInvariant().Trim().Contains(query))
                            {
                                results.Add(song);
                            }
                        }

                    if (results.Count == 0)
                        foreach (Song song in songs)
                        {
                            if (song.Album.Name.ToLowerInvariant().Trim().Contains(query))
                            {
                                results.Add(song);
                            }
                        }


                    SearchResults.ItemsSource = results;
                    SearchResults.Visibility = System.Windows.Visibility.Visible;
                    songCollection.Visibility = System.Windows.Visibility.Collapsed;
                }

                else
                {
                    SearchResults.Visibility = System.Windows.Visibility.Collapsed;
                    songCollection.Visibility = System.Windows.Visibility.Visible;
                }
            }
            catch (Exception _e)
            {
                SearchResults.Visibility = System.Windows.Visibility.Collapsed;
                songCollection.Visibility = System.Windows.Visibility.Visible;
            }
        }
    }
}

public class SongItem
{
    public String imgName { get; set; }
    public String Name { get; set; }
    public String Artist { get; set; }
}